<template>
	<view class="content">
		<scroll-view
		  @scrolltolower="scrolltolower"
		  scroll-y
		  class="content flex-h-1"
		>
		<view class="headerStyle">
			<!-- 自定义标题栏 -->
			<u-navbar :is-fixed="false" height="38px" :border-bottom="false" back-icon-color="#FFF"
				:background="background" title-color=" #FFF" title="月付">
			</u-navbar>
			<!-- 月付头部 -->
			<view class="yueTop">
				<view class="yueTopMonth pt40">
					总计待还
				</view>
				<view class="yueTopmoney">
					<text v-if="obj.money_month_used > 0">{{obj.money_month_used}}</text>
					<text  v-else>0</text>
				</view>
				<view class="xian"></view>
				<view class="bill">
					<view class="bill_txt" @click="goHistoryOrder">
						<view class="bill_txt_all">
							历史账单
						</view>
						<view class="bill_txt_mei mt5">
							每月10日还款
						</view>
					</view>
					<view class="bill_txt">
						<view class="bill_txt_all">
							总计额度
						</view>
						<view class="bill_txt_erdu mt5">
							{{ (obj.money_month - obj.money_month_used || 0).toFixed(2) || 0 }}可用
						</view>
					</view>
				</view>
			</view>
			<!--本月累计  -->
			<view class="thisMonth">
				<view class="thisMonth_header" @click="goFiveBilling">
					<view class="thisMonth_header_name">
						本月累计
					</view>
					<view class="thisMonth_header_all">
						<view class="selsecaLL">
							查看全部
						</view>
						<view class="ml10" style="margin-bottom: 2rpx;">
							<u-icon color="#8C8C8C" name="arrow-right" size="25rpx"></u-icon>
						</view>
					</view>
				</view>
				<!-- 调取接口订单获取 -->
				<view class="orderList">
					<view class="" v-for="(item,index) in list" :key="index">
						<view class="gouwu">
							<view v-if="item.type==0" class="gouwu_name">
								商城购物
							</view>
							<view v-if="item.type==1" class="gouwu_name">
								退款
							</view>
							<view v-if="item.type==2" class="gouwu_name">
								还款
							</view>
							<view class="gouwu_money">
								{{item.money}}
							</view>
						</view>
						<view class="mt14 orBian">
							{{item.order_no}}
						</view>
						<view class="mt10 orBian">
							{{timestampToTime(item.updated_at)}}
						</view>
						<view class="xian"></view>
					</view>
				</view>
			</view>
			 <u-loadmore :status="more"></u-loadmore>
		</view>
		 </scroll-view>
	</view>
</template>
<script>
	import monthlyPaymentA from "@/api/monthlyPaymentA.js";
	export default {
		data() {
			return {
				background: {
					backgroundColor: '#fffff00',
				},
				list: [],
				obj: {},
				state: '',
				month:'',
				page: 1,
				more: 'loadmore',
			}
		},
		onLoad() {
			this.getList()
			this.getCurrentYearMonth()
		},
		methods: {
			// 触发底部
			scrolltolower() {
			  this.page++;
			  this.getList();
			},
			// 获取初始化数据
			getList() {
				if (this.more == 'nomore') return;
				monthlyPaymentA
					.getPayment({
						 page:this.page,
					})
					.then((res) => {
						if (res.code == 0) {
							this.obj = res.user_info
							this.list.push(...res.data.list);
							this.more =this.list.length >= res.data.totalCount ? 'nomore' : 'loadmore';
						}
					});
			},
			// 跳转历史订单
			goHistoryOrder() {
				uni.navigateTo({
					url: "/monthlyPaymentA/historyOrder/historyOrder"
				})
			},
			// 跳转本月订单
			goFiveBilling() {
				 uni.navigateTo({
				      url: `/monthlyPaymentA/fiveBilling/fiveBilling?time=${this.state}&month=${this.month}`
				  });
			},
			timestampToTime(timestamp) {
				// 将时间戳转换为毫秒
				let time = new Date(timestamp * 1000);
				// 使用 Date 对象的方法获取年、月、日、时、分、秒
				let year = time.getFullYear();
				let month = time.getMonth() + 1; // 月份是从0开始的，所以要加1
				let day = time.getDate();
				let hours = time.getHours();
				let minutes = time.getMinutes();
				let seconds = time.getSeconds();

				// 返回格式化后的时间字符串
				return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
			},
			getCurrentYearMonth() {
				const currentDate = new Date()
				const currentYear = currentDate.getFullYear()
				const currentMonth = currentDate.getMonth() + 1 // 月份从0开始，需要加1
				const lastDayOfMonth = new Date(currentYear, currentMonth, 0).getDate()
				// 格式化日期
				const formatNumber = (num) => {
					return num < 10 ? '0' + num : num.toString()
				}
				const formattedCurrentMonth = formatNumber(currentMonth)
				const formattedLastDayOfMonth = formatNumber(lastDayOfMonth)
				// 设置begin_time为当前日期
				this.state = `${currentYear}-${formattedCurrentMonth}`
				this.month=formattedCurrentMonth
			},

		}
	}
</script>

<style>
	.orBian {
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 24rpx;
		color: #8C8C8C;
		font-style: normal;
		text-transform: none;
	}

	.mt14 {
		margin-top: 14rpx;
	}

	.mt10 {
		margin-top: 10rpx;
	}

	.gouwu {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.gouwu_money {
		font-weight: 600;
		font-size: 34rpx;
		color: #333333;
	}

	.gouwu_name {
		font-weight: 400;
		font-size: 28rpx;
		color: #262626;
	}

	.orderList {
		padding: 0rpx 24rpx;
	}

	.selsecaLL {
		font-weight: 400;
		font-size: 24rpx;
		color: #8C8C8C;
	}

	.ml10 {
		margin-left: 10rpx;
	}

	.thisMonth_header_all {
		display: flex;
		align-items: center;
	}

	.thisMonth_header_name {
		font-weight: 600;
		font-size: 30rpx;
		color: #262626;
	}

	.thisMonth {
		width: 702rpx;
		margin: 20rpx auto;
		background-color: #FFF;
		font-family: PingFang SC, PingFang SC;
		font-style: normal;
		text-transform: none;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
	}

	.thisMonth_header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 24rpx;
	}

	.content {
		min-height: 100vh;
		background-color: #F2F2F2;
	}

	.headerStyle {
		width: 750rpx;
		height: 400rpx;
		background: linear-gradient(180deg, #F0250E 60%, #F2F2F2 100%);
		border-radius: 0rpx 0rpx 0rpx 0rpx;
	}

	.yueTop {
		width: 702rpx;
		height: 336rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		font-family: PingFang SC, PingFang SC;
		font-style: normal;
		text-transform: none;
		margin: 20rpx auto;
	}

	.yueTopMonth {
		font-weight: 400;
		font-size: 26rpx;
		color: #262626;
		text-align: center;
	}

	.yueTopmoney {
		font-weight: 600;
		font-size: 64rpx;
		color: #262626;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.pt40 {
		padding-top: 40rpx;
	}

	.xian {
		width: 654rpx;
		height: 2rpx;
		background: #F8F8F8;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		margin: 30rpx auto;
	}

	.bill {
		display: flex;
		align-items: center;
	}

	.bill_txt {
		width: 351rpx;
		text-align: center;
	}

	.bill_txt_all {
		font-weight: 600;
		font-size: 30rpx;
		color: #262626;
	}

	.bill_txt_mei {
		font-weight: 400;
		font-size: 26rpx;
		color: #8C8C8C;
	}

	.bill_txt_erdu {
		font-weight: 400;
		font-size: 26rpx;
		color: #FE6F32;
	}

	.mt5 {
		margin-top: 5rpx;
	}
</style>